package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.admin.util.UserContext;
import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyContent;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.DailyQueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 *日报
 * @author root
 */
@Controller
@RequestMapping("daily")
public class DailyController {
    @Autowired
    private IDailyService dailyService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") DailyQueryObject qo, Model model){
        model.addAttribute("pageInfo",dailyService.query(qo));
        return "daily/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(MultipartFile file, Daily daily){
        User user = UserContext.getCurrentUser();
        //要先对文件做判断
        if(file != null && file.getSize()>0){
            String url = UploadUtil.upload(file, UploadUtil.PATH + "/upload");
            daily.setCoverUrl(url);
        }
        dailyService.saveOrUpdate(daily);
        return new JSONResult();
    }
    @RequestMapping("getContentById")
    @ResponseBody
    public DailyContent getContentById(Long id){
        return  dailyService.getContentById(id);
    }
}